//
//  DevChartTypeModel.m
//  RuiHeApp
//
//  Created by Jun on 2022/11/24.
//

#import "DevChartTypeModel.h"
#import "DevChartParamsModel.h"
#import "SYChartTool.h"
#import "DevChartModel.h"
#import "DeviceChartFieldModel.h"

@implementation DevChartTypeModel

+ (NSArray *)creatTypesData{
    NSMutableArray *array = [NSMutableArray array];
    NSArray *types = @[@(ChartType_Master),
                       @(ChartType_EMS),
                       @(ChartType_PACK),
                       @(ChartType_Inverter),
                       @(ChartType_PCS),
                       @(ChartType_DCDCIN),
                       @(ChartType_DCDCOUT),
                       @(ChartType_PCContr),
                       @(ChartType_COMBOX),
                       @(ChartType_SMARTSW)];
    NSArray *titles = @[@"Master",
                        @"EMS",
                        @"Pack",
                        @"Inverter",
                        @"PCS",
                        @"DCDCIN",
                        @"DCDCOUT",
                        @"PV controller",
                        @"Combiner box",
                        @"Smart switch"];
    NSArray *images = @[@"master_icon",
                        @"switch_icon",
                        @"pack_icon",
                        @"inverter_icon",
                        @"pcs_icon",
                        @"dcdcin_icon",
                        @"dcdcout_icon",
                        @"pv_icon",
                        @"combiner_icon",
                        @"switch_icon"];
    
    [types enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
       
        DevChartTypeModel *model = [[DevChartTypeModel alloc] init];
        model.type = (DevChartType)[obj integerValue];
        model.icon_name = images[idx];
        model.title = titles[idx];
        if (model.type == ChartType_PACK) {
            model.packNum = 1;
        }
        [array addObject:model];
    }];
    
    return array;
}


- (DevChartParamsModel *)creatChartParams{
    NSArray *filesName = @[@"MasterChartParams",
                           @[@"InverterChart_Solar",@"InverterChart_Grid",@"InverterChart_Load"],
                           @"",
                           @"PCSChartParams",
                           @"PackChartParams",
                           @"DCDCINChartParams",
                           @"DCDCOUTChartParams",
                           @"",
                           @"",
                           @"EMSChartParams"];
    NSString *jsonName;
    
    if (self.type<filesName.count) {
        if ([filesName[self.type] isKindOfClass:[NSArray class]]) {
            jsonName = [(NSArray *)filesName[self.type] objectAtIndex:self.inverTerType];
        }else{
            jsonName = filesName[self.type];
        }
    }
    
    DevChartParamsModel *model;
    if (ValidStr(jsonName)) {
        NSDictionary *dict = [SYChartTool readLocalJsonFileWithName:jsonName];
        model = [DevChartParamsModel yy_modelWithDictionary:dict];
        if (self.type == ChartType_PACK) {
            model.packNum = self.packNum;
        }
        if (self.type == ChartType_Inverter) {
            model.inverTerType = self.inverTerType;
        }
        
        model.type = self.type;
        
        
        //从本地json文件初始化曲线
        NSMutableArray *list = [NSMutableArray arrayWithArray:dict[@"chartParams"]];
        NSMutableArray *arr = [NSMutableArray array];
        for (NSArray *array in list) {
            DevChartModel *chartModel = [[DevChartModel alloc] init];
            chartModel.dayParams = [NSArray yy_modelArrayWithClass:[DeviceChartFieldModel class] json:array];
            chartModel.monthParams = [NSArray yy_modelArrayWithClass:[DeviceChartFieldModel class] json:array];
            chartModel.yearParams = [NSArray yy_modelArrayWithClass:[DeviceChartFieldModel class] json:array];
            chartModel.params = [chartModel.dayParams mutableCopy];
            [arr addObject:chartModel];
            
        }
        model.chartList = arr.copy;
    }
    NSLog(@"----->加载的文件名:%@",jsonName);
    return model;
}

+ (DevChartParamsModel *)creatChartParams:(DevChartType)type{
    NSArray *filesName = @[@"MasterChartParams",
                           @"InverterChartParams",
                           @"",
                           @"PCSChartParams",
                           @"PackChartParams",
                           @"DCDCINChartParams",
                           @"DCDCOUTChartParams",
                           @"",
                           @""];
    NSString *jsonName;
    
    if (type<filesName.count) {
        jsonName = filesName[type];
    }
    
    DevChartParamsModel *model;
    if (ValidStr(jsonName)) {
        NSDictionary *dict = [SYChartTool readLocalJsonFileWithName:jsonName];
        model = [DevChartParamsModel yy_modelWithDictionary:dict];
        if (type == ChartType_PACK) {
            model.packNum = 1;
        }
        model.type = type;
    }
    
    return model;
}

-(void)setTitle:(NSString *)title
{
    _title = title;
    NSDictionary *iconDic = @{@"EMS":@"switch_icon",
                              @"Master":@"master_icon",
                              @"PCS":@"pcs_icon",
                              @"Pack":@"pack_icon",
                              @"Inverter":@"inverter_icon",
                              @"DCDCIN":@"dcdcin_icon",
                              @"DCDCOUT":@"dcdcout_icon"
    };
  
    NSDictionary *typeDic = @{@"EMS":@(ChartType_EMS),
                              @"Master":@(ChartType_Master),
                              @"PCS":@(ChartType_PCS),
                              @"Pack":@(ChartType_PACK),
                              @"Inverter":@(ChartType_Inverter),
                              @"DCDCIN":@(ChartType_DCDCIN),
                              @"DCDCOUT":@(ChartType_DCDCOUT)
    };
    _icon_name = [iconDic objectForKey:title];
    _type = (DevChartType)[[typeDic objectForKey:title] integerValue];

    
}
@end
